package com.ruoyi.web.controller.wxf;

import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;
import ucar.nc2.Dimension;
import ucar.nc2.Attribute;

import java.io.IOException;
import java.util.List;

public class HDF4ReaderNetCDF {
    public static void main(String[] args) {
        String filePath = "G:\\MYD021KM\\2024\\MYD021KM.A2024001.0640.061.2024001191059.hdf";

        // 使用 try-with-resources 语句自动关闭文件
        try (NetcdfFile ncFile = NetcdfFile.open(filePath)) {
            // 打印文件基本信息
            System.out.println("文件路径: " + ncFile.getLocation());
            System.out.println("文件类型: " + ncFile.getFileTypeId() + " (" + ncFile.getFileTypeDescription() + ")");
            System.out.println("文件全局属性:");

            // 打印全局属性
            for (Attribute attr : ncFile.getGlobalAttributes()) {
                System.out.println("  " + attr.getName() + " = " + attr.getStringValue());
            }

            // 打印维度信息
            System.out.println("\n文件维度:");
            List<Dimension> dimensions = ncFile.getDimensions();
            for (Dimension dim : dimensions) {
                System.out.println("  " + dim.getName() + " (长度: " + dim.getLength() + ")");
            }

            // 打印变量信息
            System.out.println("\n文件变量:");
            List<Variable> variables = ncFile.getVariables();
            for (Variable var : variables) {
                System.out.println("\n  变量名: " + var.getFullName());
                System.out.println("    数据类型: " + var.getDataType());
//                System.out.println("    形状: " + var.getShapeAsString());
                System.out.println("    维度: " + var.getDimensionsString());

                // 打印变量属性
                System.out.println("    属性:");
                for (Attribute attr : var.getAttributes()) {
                    System.out.println("      " + attr.getName() + " = " + attr.getStringValue());
                }

                // 读取并打印变量的前10个元素(如果是数值型数组)
                if (var.getRank() > 0 && var.getSize() > 0) {
                    try {
                        int maxElementsToRead = Math.min(10, (int) var.getSize());
                        Object data = var.read().reduce().get1DJavaArray(var.getDataType().getPrimitiveClassType());

                        System.out.print("    前" + maxElementsToRead + "个元素: ");
                        if (data instanceof byte[]) {
                            byte[] arr = (byte[]) data;
                            for (int i = 0; i < Math.min(maxElementsToRead, arr.length); i++) {
                                System.out.print(arr[i] + " ");
                            }
                        } else if (data instanceof short[]) {
                            short[] arr = (short[]) data;
                            for (int i = 0; i < Math.min(maxElementsToRead, arr.length); i++) {
                                System.out.print(arr[i] + " ");
                            }
                        } else if (data instanceof int[]) {
                            int[] arr = (int[]) data;
                            for (int i = 0; i < Math.min(maxElementsToRead, arr.length); i++) {
                                System.out.print(arr[i] + " ");
                            }
                        } else if (data instanceof long[]) {
                            long[] arr = (long[]) data;
                            for (int i = 0; i < Math.min(maxElementsToRead, arr.length); i++) {
                                System.out.print(arr[i] + " ");
                            }
                        } else if (data instanceof float[]) {
                            float[] arr = (float[]) data;
                            for (int i = 0; i < Math.min(maxElementsToRead, arr.length); i++) {
                                System.out.print(arr[i] + " ");
                            }
                        } else if (data instanceof double[]) {
                            double[] arr = (double[]) data;
                            for (int i = 0; i < Math.min(maxElementsToRead, arr.length); i++) {
                                System.out.print(arr[i] + " ");
                            }
                        } else if (data instanceof String[]) {
                            String[] arr = (String[]) data;
                            for (int i = 0; i < Math.min(maxElementsToRead, arr.length); i++) {
                                System.out.print(arr[i] + " ");
                            }
                        }
                        System.out.println();
                    } catch (IOException e) {
                        System.out.println("    读取数据时出错: " + e.getMessage());
                    }
                }
            }

        } catch (IOException e) {
            System.err.println("打开文件时出错: " + e.getMessage());
            e.printStackTrace();
        }
    }
}